#include "changestaffwindow.h"
#include "ui_changestaffwindow.h"
#include"managewindow.h"
#include<QListWidgetItem>
#include"staffmanageform.h"
#include "staffmanagerform2.h"
#include<QDebug>
#include<QMessageBox>
#include<QDialog>
#include<QInputDialog>
#include<QFile>
#define serverPath "./data/serverIP.txt"
#define SERVER_PROT 6065
changeStaffWindow::changeStaffWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::changeStaffWindow)
{
    ui->setupUi(this);

    this->socket=new QTcpSocket;
    QListWidgetItem *aitem=new QListWidgetItem;
    aitem->setSizeHint(QSize(500,40));
    ui->listWidget->addItem(aitem);
    staffManageForm *From=new staffManageForm;
    ui->listWidget->setItemWidget(aitem,From);


    connect(socket,&QIODevice::readyRead,this,&changeStaffWindow::readData);
    connect(socket,&QTcpSocket::connected,this,&changeStaffWindow::connectedTohost);
    QFile *server=new QFile(serverPath);
   if(!server->open(QIODevice::ReadOnly))
   {
        qDebug()<<"open serverPath failed";
   }
  QByteArray arr=server->readAll();

    socket->connectToHost(QString(arr),SERVER_PROT);


}

changeStaffWindow::~changeStaffWindow()
{
    delete ui;
}

void changeStaffWindow::deleteStaff(const QString &who)
{
    QByteArray delData(QString("del#"+who).toUtf8());
    socket->write(delData);
    qDebug()<<"删除员工:"<<who;
}



void changeStaffWindow::on_returnButton_clicked()
{
    QObject* before=this->parent();
    manageWindow *p=dynamic_cast<manageWindow *>(before);
    p->show();
    this->close();
}

void changeStaffWindow::connectedTohost()
{
    QByteArray data1= QByteArray("getStaff#");
    socket->write(data1);
}

void changeStaffWindow::readData()
{

    QByteArray newData= socket->readAll();

    QString data(newData);

    if(data.split(":")[0]=="删除成功")
    {
        int i=0;
        for(;i<namelist.size();i++)
        {
            if(namelist[i]==data.split(":")[1])
             {
                break;
            }
        }
        ui->listWidget->takeItem(i+1);
        QMessageBox::information(this,"删除结果","删除成功");

    }
    else if(data.split(":")[0]=="添加失败")
    {
       QMessageBox::information(this,"添加结果","添加失败：此用户已存在");
    }
    else if(data.split(":")[0]=="添加成功")
    {
       QMessageBox::information(this,"添加结果","添加成功");
       ui->listWidget->clear();
       QByteArray data1= QByteArray("getStaff#");
       socket->write(data1);

    }
    else
    {
        QStringList staffList= data.split("+");

        for(int i=1;i<staffList.size();i++)
        {
            QListWidgetItem *aitem1=new QListWidgetItem;
            aitem1->setSizeHint(QSize(500,40));
            ui->listWidget->addItem(aitem1);
            staffManagerForm2 *From=new staffManagerForm2(staffList[i].split(':')[0],
                    staffList[i].split(':')[1],staffList[i].split(':')[2],this);
            namelist<<staffList[i].split(':')[0];
            ui->listWidget->setItemWidget(aitem1,From);

        }
    }





}

void changeStaffWindow::on_addStaffButton_clicked()
{
    if(ui->comboBox->currentText()=="管理员")
    {
        bool ok;
             QString text = QInputDialog::getMultiLineText(this,"请输入信息",
                                                           "请输入新管理员名字和密码并用#隔开:", "", &ok);
        qDebug()<<QString("add#"+text).toUtf8();
         QByteArray newStaff=(QString("add#"+text+"#管理员").toUtf8());
         socket->write(newStaff);
    }
    else
    {
        bool ok;
             QString text = QInputDialog::getMultiLineText(this,"请输入信息",
                                                           "请输入新员工名字和密码并用#隔开:", "", &ok);
             qDebug()<<QString("add#"+text).toUtf8();
             QByteArray newStaff=(QString("add#"+text+"#员工").toUtf8());
             socket->write(newStaff);
    }


}
